<!DOCTYPE html>
<html>
<head>
    <title>gimi2.0-域名批量注册</title>
    <meta charset="UTF-8">
    <style>
        body { font-family: sans-serif; }
        #results { margin-top: 20px; }
        .result-item { border: 1px solid #ccc; padding: 10px; margin-bottom: 10px; }
        .result-item .price { font-weight: bold; }
        .result-item .status { margin-left: 10px; }
    </style>
</head>
<body>

<h1>域名批量注册</h1>

<textarea id="domain-list" placeholder="每行一个域名"></textarea><br>
<button onclick="checkDomains()">检查域名</button>

<div id="results"></div>

<script>
    const API_URL = '/api/register'; // 定义常量 URL

    async function checkDomains() {
        const domainList = document.getElementById('domain-list').value.split('\n').filter(Boolean);
        const resultsDiv = document.getElementById('results');
        resultsDiv.innerHTML = ''; // 清空上次结果

        if (domainList.length === 0) {
            alert("请输入域名");
            return;
        }

        for (const domain of domainList) {
            const resultItem = document.createElement('div');
            resultItem.className = 'result-item';
            resultItem.innerHTML = `<span>${domain}：检查中...</span>`;
            resultsDiv.appendChild(resultItem);

            try {
                const response = await fetch(`${API_URL}?domain=${domain}`);
                const data = await response.json();
                console.log(data);
                if (!response.ok){
                    throw new Error(data.message || "请求失败")
                }
                resultItem.innerHTML = `
        <span>${domain}</span>
        <span class="price">价格：${data.price || '未知'}</span>
        <span class="status">${data.available ? '可注册' : '不可注册'}</span>
        ${data.available ? `<button onclick="registerDomain('${domain}')">立即注册</button>` : ''}
      `;

            } catch (error) {
                console.error(`检查 ${domain} 出错：`, error);
                resultItem.innerHTML = `
        <span>${domain}</span>
        <span class="status" style="color: red">检查出错：${error.message}</span>
      `;
            }
        }
    }

    async function registerDomain(domain) {
        try {
            const response = await fetch(`${API_URL}/register?domain=${domain}`,{method: 'POST'});
            const data = await response.json();
            if (!response.ok){
                throw new Error(data.message || "注册失败")
            }
            alert(`${domain} 注册${data.success?"成功":"失败"},${data.message}`);
        } catch (error) {
            alert(`${domain} 注册失败：${error.message}`);
        }
    }
</script>

</body>
</html>